package de.hibernate.example;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table (name="error_log")
public class ErrorLog {
	
	@SuppressWarnings("rawtypes")
	private Class throwableClass;
	
	private StackTraceElement[] stackTrace;
	private String message;
	
	
	private final static int depth = 0;
	
	public ErrorLog(Throwable t) {
		this.throwableClass = t.getClass();
		this.stackTrace = t.getStackTrace();
		this.message = t.getMessage();
	}

	@Id
	@Column
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Column
	public String getThrowableClass() {
		String className = this.throwableClass.getName();
		return className;
	}

	public void setThrowableClass(@SuppressWarnings("rawtypes") Class throwableClass) {
		this.throwableClass = throwableClass;
	}

	@Transient
	public String getStackTrace() {
		String stackTraceString = stackTrace[0].toString();
		for(int i = 1;i<=depth;i++) {
			stackTraceString += stackTrace[i].toString();	
		}
		return stackTraceString;
	}

	public void setStackTrace(StackTraceElement[] stackTrace) {
		this.stackTrace = stackTrace;
	}
	
}
